package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.DiscountAdapter
import com.cgbl.cg.adapter.NewRecommendAdapter
import com.cgbl.cg.adapter.PopularMaterialAdapter
import com.cgbl.cg.databinding.AppFragmentHomeResourceBinding
import com.cgbl.cg.ui.WebActivity
import com.cgbl.cg.vm.home.HomeResourceViewModel
import com.robot.base.base.activity.GLBaseFragment
import com.robot.base.model.BannerBean
import com.robot.base.model.ResourceBean
import com.robot.base.model.RibbonBean
import com.robot.base.utils.pic.ImageLoaderUtils
import com.robot.base.view.DividerItemDecoration
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder

class HomeResourceFragment :
    GLBaseFragment<AppFragmentHomeResourceBinding, HomeResourceViewModel>() {
    private lateinit var bannerTitleAdapter: BannerImageAdapter<BannerBean>

    private lateinit var newRecommendAdapter: NewRecommendAdapter
    private lateinit var popularMaterialAdapter: PopularMaterialAdapter
    private lateinit var discountAdapter: DiscountAdapter

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_home_resource

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initBanner()
        initAdapter()
    }

    override fun initData() {
        super.initData()
        viewModel.refresh()
    }

    private fun initBanner() {
        bannerTitleAdapter = object : BannerImageAdapter<BannerBean>(
            arrayListOf()
        ) {
            override fun onBindView(
                holder: BannerImageHolder?,
                data: BannerBean?,
                position: Int,
                size: Int
            ) {
                    ImageLoaderUtils.getGlideManager()
                        .load(data?.imagesFile)
                        .apply(ImageLoaderUtils.getRoundCornerGlideOption(10))
                        .into(holder?.imageView!!)
            }
        }
        binding.banner.setAdapter(bannerTitleAdapter)
        binding.banner.setIndicator(binding.indicator, false)
        binding.banner.setBannerGalleryEffect(12, 4)
        binding.banner.start()

        bannerTitleAdapter.setOnBannerListener { data, position ->
            WebActivity.start(requireContext(), data.hrefUrl ?: "", data?.coverName ?: "")
        }
    }

    private fun initAdapter() {
        newRecommendAdapter = NewRecommendAdapter()
        binding.recyclerViewNewRecommend.apply {
            adapter = newRecommendAdapter
            addItemDecoration(
                DividerItemDecoration(
                    requireContext(),
                    LinearLayoutManager.VERTICAL,
                    0.5f,
                    R.color.app_F7F6FA
                )
            )
        }

        discountAdapter = DiscountAdapter(arrayListOf())
        binding.bannerDiscount.setAdapter(
            discountAdapter
        )

        binding.bannerDiscount.setBannerGalleryEffect(84, 0)
        binding.bannerDiscount.start()

        popularMaterialAdapter = PopularMaterialAdapter()
        binding.recyclerViewMoreResource.apply {
            adapter = popularMaterialAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
//        binding.smartRefreshLayout.setOnLoadMoreListener {
//            it.finishLoadMore()
//            viewModel.toResourceMenuCommand.execute()
//        }

        viewModel.uc.ribbonDataEvent.observe(this) {
            bannerTitleAdapter.setDatas(it)
        }

        viewModel.uc.recommendDataEvent.observe(this) {
            newRecommendAdapter.setNewInstance(it as MutableList<ResourceBean>?)
        }

        viewModel.uc.popularMaterialDataEvent.observe(this) {
            popularMaterialAdapter.setNewInstance(it as MutableList<ResourceBean>?)
        }

        viewModel.uc.limitedDataEvent.observe(this){
            discountAdapter.setDatas(it)
        }
    }
}